var ns, toast, imageBrowser, UIInput;
var app = new Vue({
    el: '#app',
    data: {
        form: {
            id: 0,
            Name: '',
            AddTime: '',
            GroupName: '',
            Content: '',
            headimage: '../image/default_user.png'
        },
        imglist: [],
        comment: {
            Content: '',
            ResponseUserID: 0
        },
        commentlist: [],
        UIInputID: -1,
        havenocomments: false,
        show_keyboard: false,
        selectId: 0,
        timeOutEvent: 0,
    },
    methods: {
        get_data: function () {
            var that = this;
            that.is_searching = true;
            that.can_scroll = false;
            that.selectId = 0;
            var options = {};
            options.action = 'getthreaddetail';
            options.ID = that.form.id;
            ns.post(options, function (succeed, data, err) {
                console.log(JSON.stringify(options));
                console.log(JSON.stringify(data));
                if (succeed) {
                    that.form = data.form;
                    that.imglist = data.imglist;
                    for (var i = 0; i < that.imglist.length; i++) {
                        that.image_cache(that.imglist[i], i);
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        gettouchStart: function (id, UserID) {
            //长按弹出操作框
            var that = this;
            that.selectId=0;
            that.timeOutEvent = setTimeout(function () {
                ns.post({
                    UserID: UserID,
                    ThreadID: id,
                    action: 'CannotOperate',
                }, function (succeed, data, err) {
                    if (succeed) {
                        if (data.status) {
                            that.selectId = id;
                        } else {
                            api.toast({
                                msg: '不能进行此操作',
                                duration: 2000,
                                location: 'bottom'
                            });
                            api.removeEventListener({
                                name: 'longpress'
                            })
                        }
                    }
                });

            }, 500)
        },
        gettouchmove: function () {
            var that = this;
            clearTimeout(that.timeOutEvent);
            that.timeOutEvent = 0;
        },
        getttouchend: function () {
            var that = this;
            clearTimeout(that.timeOutEvent);
        },
        doHideSearch: function () {
            var that = this;
            that.selectId = 0;
        },


        get_comments: function () {
            var that = this;
            that.havenocomments = false;
            var options = {};
            options.action = 'getthreadcommentlist';
            options.ID = that.form.id;
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.commentlist = data.commentlist;
                    if (that.commentlist.length == 0) {
                        that.havenocomments = true;
                    }
                } else if (err) {
                    that.havenocomments = true;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        image_cache: function (item, index) {
            var that = this;
            if (that.imglist.length == 0) {
                return;
            }
            api.imageCache({
                url: item.url,
                thumbnail: false
            }, function (ret, err) {
                if (ret.status) {
                    that.imglist[index].cacheurl = ret.url;
                }
            });
        },
        img_browser: function (index) {
            var that = this;
            var imgurls = [];
            for (var i = 0; i < that.imglist.length; i++) {
                imgurls.push(that.imglist[i].url);
            }
            imageBrowser.openImages({
                imageUrls: imgurls,
                showList: false,
                activeIndex: index
            });
        },
        jubao: function (UserID, id, PrijectID) {
            var that = this;
            ns.openWin('dl_bbsjubao_frm', '举报', {
                ThreadID: id,
                PrijectID: PrijectID,
                ReportedUserID: UserID,
                type: 2,
                postType: 1
            });
            that.selectId = 0;
        },
        addto_black: function (UserID, ProjectID) {
            var that = this;
            that.selectId = 0;
            api.confirm({
                title: '提示',
                msg: '拉黑用户后，您将无法查看和接受该用户的发布信息、回复信息。后续可以在【我的收藏-黑名单】中取消拉黑。是否确认操作？',
                buttons: ['确认拉黑', '取消操作']
            }, function (ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        ns.post({
                            Type: 1,
                            ReportedUserID: UserID,
                            ProjectID: ProjectID,
                            action: 'savamallreporttype',
                        }, function (succeed, data, err) {
                            if (succeed) {
                                api.toast({
                                    msg: '已进入黑名单',
                                    duration: 2000,
                                    location: 'bottom'
                                });
                                api.sendEvent({
                                    name: 'updatedianzan'
                                });

                            } else if (err) {
                                api.toast({
                                    msg: err,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        });
                    };
                };
            });
        },
        do_praise: function (type) {
            var that = this;
            ns.post({
                action: 'postornot',
            }, function (succeed, data, err) {
                if (succeed) {
                    if (data.IsActive) {
                        that.do_open_praise(type)
                    } else {
                        api.toast({
                            msg: "不能点赞",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                }
            });

        },

        do_open_praise: function (type) {
            var that = this;
            if (!that.form.cancomment) {
                return;
            }
            if (that.form.userforbiddened) {
                api.toast({
                    msg: '您已禁言',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            ns.post({
                id: that.form.id,
                Type: type,
                action: 'postthreadpraise'
            }, function (succeed, data, err) {
                console.log(JSON.stringify(data));
                if (succeed) {
                    if (type == 0) {
                        that.form.ispraised = data.IsPraise;
                        that.form.dianzan = data.count;
                    }
                    if (type == 1) {
                        that.form.iscancomment = data.IsPraise;
                    }


                    api.sendEvent({
                        name: 'updatedianzan'
                    });

                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        //收藏
        do_collectposts: function (type) {
            var that = this;
            ns.post({
                action: 'postornot',
            }, function (succeed, data, err) {
                if (succeed) {
                    if (data.IsActive) {
                        that.do_open_collectposts(type)
                    } else {
                        api.toast({
                            msg: "不能收藏",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                }
            });

        },
        do_open_collectposts: function (type) {
            var that = this;
            // if (that.form.ispraised) {
            //     return;
            // }
            if (!that.form.cancomment) {
                return;
            }
            if (that.form.userforbiddened) {
                api.toast({
                    msg: '您已禁言',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            ns.post({
                id: that.form.id,
                Type: type,
                action: 'getcollectposts'
            }, function (succeed, data, err) {
                console.log(JSON.stringify(data));
                if (succeed) {
                    that.form.iscancomment = data.IsCollection;
                    api.sendEvent({
                        name: 'updatedianzan'
                    });

                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        //发送
        do_post: function () {
            var that = this;
            ns.post({
                action: 'postornot',
            }, function (succeed, data, err) {
                if (succeed) {
                    if (data.IsActive) {
                        that.do_openpost();
                    } else {
                        api.toast({
                            msg: "不能评论",
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.comment.Content = "";
                    }
                }
            });

        },

        do_openpost: function () {
            var that = this;
            if (!that.form.cancomment) {
                return;
            }
            if (that.form.userforbiddened) {
                api.toast({
                    msg: '您已禁言',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.comment.Content == '') {
                that.hide_keyboard();
                api.toast({
                    msg: '您什么也没留下',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            ns.post({
                id: that.form.id,
                action: 'postthreadcomment',
                Content: that.comment.Content,
                ResponseUserID: that.comment.ResponseUserID
            }, function (succeed, data, err) {
                if (succeed) {
                    that.get_comments();
                    that.comment.Content = '';
                    setTimeout(function () {
                        that.hide_keyboard();
                    }, 200);
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },

        //评论
        start_post: function (UserID, UserName) {
            var that = this;
            ns.post({
                action: 'postornot',
            }, function (succeed, data, err) {
                if (succeed) {
                    if (data.IsActive) {
                        that.do_openstart_post(UserID, UserName);
                    } else {
                        api.toast({
                            msg: "不能评论",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                }
            });

        },

        do_openstart_post: function (UserID, UserName) {
            var that = this;
            ns.post({
                action: 'postornot',
            }, function (succeed, data, err) {
                if (succeed) {
                    if (!data.IsActive) {
                        api.toast({
                            msg: "不能评论",
                            duration: 2000,
                            location: 'bottom'
                        });
                        return;
                        that.get_data();

                    }

                }
            });

            if (!that.form.cancomment) {
                return;
            }
            if (that.form.userforbiddened) {
                api.toast({
                    msg: '您已禁言',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.comment.Content) {
                return;
            }
            that.show_keyboard = true;
            var el = $api.byId('textarea')
            if (UserID > 0) {
                $api.attr(el, 'placeholder', '回复用户[' + UserName + ']...');
            } else {
                $api.attr(el, 'placeholder', '写点什么吧...');
            }
            that.comment.Content = '';
            that.comment.ResponseUserID = UserID;
            setTimeout(function () {
                that.resize_textarea();
            }, 200);
            if (api.systemType != 'ios') {
                setTimeout(function () {
                    that.open_input(true);
                }, 100);
                return;
            }
            that.open_input(true);
        },
        open_input: function (autoFocus) {
            var that = this;
            setTimeout(function () {
                var textarea = document.getElementById("textarea");
                if (textarea) {
                    textarea.focus();
                }
            }, 200);
            if (that.UIInputID == 0) {
                UIInput.value({
                    msg: ''
                });
                UIInput.show({
                    id: that.UIInputID
                });
                return;
            }
            UIInput.open({
                rect: {
                    x: 0,
                    y: 0,
                    w: 0,
                    h: 0
                },
                styles: {
                    bgColor: '#fff',
                    size: 14,
                    color: '#000',
                    placeholder: {
                        color: '#ccc'
                    }
                },
                autoFocus: autoFocus,
                maxRows: 4,
                placeholder: '写点什么吧...',
                keyboardType: 'text',
                fixedOn: api.frameName
            }, function (ret) {
                if (ret.eventType == 'change') {
                    UIInput.value(function (rec) {
                        if (rec) {
                            that.comment.Content = rec.msg;
                        }
                    });
                } else {
                    that.UIInputID = ret.id;
                }
            });
        },
        resize_textarea: function () {
            var text = document.getElementById("textarea");
            if (text) {
                autoTextarea(text, 0, 100);
            }
        },
        initial_setup: function () {
            var that = this;
            that.get_data();
            that.get_comments();
            that.resize_textarea();
            that.open_input(false);
        },
        hide_keyboard: function () {
            var that = this;
            that.show_keyboard = false;
            that.comment.ResponseUserID = 0;
            UIInput.hide({
                id: that.UIINputID
            });
            that.resize_textarea();
            var el = $api.byId('textarea');
            $api.attr(el, 'placeholder', '写点什么吧...');
        }
    }
});
apiready = function () {
    api.parseTapmode();
    imageBrowser = api.require('imageBrowser');
    UIInput = api.require('UIInput');
    ns = window.Foresight.Util;
    toast = new auiToast();
    app.form.id = api.pageParam.id;
    app.initial_setup();
    api.addEventListener({
        name: 'open_jubao_input'
    }, function (ret, err) {
        if (ret) {
            app.selectId = 0;
            ns.post({
                UserID: app.form.UserID,
                ThreadID: app.form.id,
                action: 'CannotOperate',
            }, function (succeed, data, err) {
                if (data.status) {
                    ns.openWin('dl_bbsjubao_frm', '举报', {
                        ThreadID: app.form.id,
                        PrijectID: app.form.PrijectID,
                        ReportedUserID: app.form.UserID,
                        type: 2,
                        postType: 2
                    });
                } else {
                    api.toast({
                        msg: '不能进行此操作',
                        duration: 2000,
                        location: 'bottom'
                    });
                }

            });


        }
    });
};
